function bwtSubmenuOver(obj, appearClick){
	/** Clearing the timeouts **/
	pNode = obj.parentNode;
	while(true){
		if(pNode.nodeName=="UL"){
			clearTimeout(pNode.hideTimer);
			pNode.hasHideTimer = false;
		}
		if(pNode.nodeName=="LI" && pNode.childNodes.length && pNode.childNodes[0].nodeName=="A"){
			pNode.childNodes[0].className = obj.activeClass;
		}
		pNode=pNode.parentNode;
		if(pNode.id==obj.listId){
			break;
		}
	}
	if(!obj.nextSibling) return;
	var node = obj.nextSibling;
	while(node){
		if(node.nodeName=="UL"){
			clearTimeout(node.hideTimer);
			if(obj.appearStyle=="click")obj.href = "javascript:void(0)";
			node.hasHideTimer = false;
			if(appearClick || obj.appearStyle=="over")bwtSubmenuShow(obj.menuObjName+".lists["+node.listIndex+"]");
		}
		node = node.nextSibling;
	}
}
function bwtSubmenuOut(obj){
	for(var i=0; i<obj.numLists; i++){
		listObj = eval(obj.menuObjName+".lists["+i+"]");
		if(!listObj.hasHideTimer && listObj.style.display=="block"){
			listObj.hideTimer = setTimeout(obj.menuObjName+".lists["+i+"].style.display='none'",listObj.hideDelay);
			listObj.hasHideTimer = true;
		}
	}
	pNode = obj.parentNode;
	while(true){
		if(pNode.nodeName=="LI" && pNode.childNodes.length && pNode.childNodes[0].nodeName=="A"){
			pNode.childNodes[0].className = '';
		}
		if(pNode.id==obj.listId)break;
		pNode=pNode.parentNode;
	}
}
function bwtSubmenuShow(index){
	var obj = eval(index);
	if(obj.style.display=="block") return;
	obj.style.display="block";
	if(obj.clipHorizontal || obj.clipVertical){
		if(!obj.clipHorizontal)obj.clipWidth=obj.offsetWidth;
		if(!obj.clipVertical)obj.clipHeight=obj.offsetHeight;
		obj.style.clip="rect(0px "+obj.clipWidth+"px "+obj.clipHeight+"px 0px)";
		bwtSubmenuClip(index);
	}
	if(obj.fadingType==1 || obj.fadingType==3){
		if(obj.isIe && false){
		}else{
			bwtSetOpacity(obj, obj.fadingStatus);
			bwtSubmenuFading(index);
		}
	}
}
function bwtSubmenuClip(index){
	var obj = eval(index);
	obj.clipHeight+=obj.clipIncrement;
	obj.clipWidth+=obj.clipIncrement;
	if(obj.clipHeight>obj.offsetHeight) obj.clipHeight=obj.offsetHeight;
	if(obj.clipWidth>obj.offsetWidth) obj.clipWidth=obj.offsetWidth;
	if(obj.clipHeight==obj.offsetHeight && obj.clipWidth==obj.offsetWidth){
		if(obj.isSafari)obj.style.clip="rect(100% 100% 100% 100%)";
		else obj.style.clip="rect(auto auto auto auto)";
		obj.clipHeight=0;
		obj.clipWidth=0;
		return;}
	obj.style.clip="rect(0px "+obj.clipWidth+"px "+obj.clipHeight+"px 0px)";
	if(obj.clipHeight<obj.offsetHeight || obj.clipWidth<obj.offsetWidth)setTimeout("bwtSubmenuClip('"+index+"')",obj.clipDelay);
}
function bwtSubmenuFading(index){
	var obj = eval(index);
	obj.fadingStatus+=obj.fadingStep;
	if(obj.fadingStatus>obj.fadingTarget)obj.fadingStatus=obj.fadingTarget;
	bwtSetOpacity(obj, obj.fadingStatus);
	//obj.style.opacity = obj.fadingStatus;
	if(obj.fadingStatus<obj.fadingTarget) setTimeout("bwtSubmenuFading('"+index+"')", obj.fadingDelay);
	else obj.fadingStatus = 0;
}
function bwtSetOpacity(obj, opacity){
	if(obj.isIe){
		opacity=parseInt(opacity*100);
		obj.style.filter = "alpha(opacity="+opacity+")";
		//	filters.alpha.Opacity = opacity;
	}else{
		obj.style.opacity=opacity
	}
}
function bwtMenu(listId, objName){
	this.subindicator = null;
	this.menu = document.getElementById(listId);
	this.depth = 1;
	this.links = this.menu.getElementsByTagName('a');
	this.lists = this.menu.getElementsByTagName('ul');
	this.clipVertical = false;
	this.clipHorizontal = false;
	this.clipDelay = 20;
	this.clipIncrement = 4;
	this.fadingType = 0; // options 0,1,2,3 means No, Fade In, Fade out, Both
	this.fadingDelay = 20;
	this.fadingSteps = 50;
	this.hideDelay = 1000;
	
	this.appearStyle = "over"; //options: click, over
	
	this.ua = navigator.userAgent.toLowerCase();
	this.isIe = document.all;
	this.isSafari = this.ua.indexOf('safari') != -1;

	this.activeClass = "highlighted";
	this.initialise = function(){
		for(var i=0;i<this.links.length; i++){
			if(this.appearStyle=="click"){
				this.links[i].onclick = function(){bwtSubmenuOver(this, true);}
			}
			this.links[i].subindicator=this.subindicator;
			this.links[i].onmouseover = function(){bwtSubmenuOver(this, false);}
			this.links[i].onmouseout = function(){bwtSubmenuOut(this);}
			this.links[i].menuObjName = objName;
			this.links[i].numLists = this.lists.length;
			this.links[i].listId = listId;
			this.links[i].appearStyle = this.appearStyle;
			this.links[i].activeClass = this.activeClass;
		}
		for(var i=0;i<this.lists.length;i++){
			this.lists[i].listIndex=i;
			this.lists[i].clipVertical = this.clipVertical;
			this.lists[i].clipHorizontal = this.clipHorizontal;
			this.lists[i].clipDelay = this.clipDelay;
			this.lists[i].clipIncrement = this.clipIncrement;
			this.lists[i].clipHeight=0;
			this.lists[i].clipWidth=0;
			
			this.lists[i].fadingType = this.fadingType;
			this.lists[i].fadingDelay = this.fadingDelay;
			this.lists[i].fadingSteps = this.fadingSteps;
			this.lists[i].fadingTarget=1;
			this.lists[i].fadingStatus=0;
			this.lists[i].fadingStep=1/this.fadingSteps;
			
			this.lists[i].hideDelay = this.hideDelay;
			this.lists[i].isIe = this.isIe;
			this.lists[i].isSafari = this.isSafari;
			if(this.subindicator){
				var siblings = this.lists[i].parentNode.childNodes;
				for(var j=0; j<siblings.length; j++){
					if(siblings[j].nodeName=="A") siblings[j].insertBefore(this.subindicator.cloneNode(true),siblings[j].firstChild);
				}
			}
		}
	}
}
